interface NetworkStatusRes {
  isConnected: boolean
  networkType: 'wifi' | '2g' | '3g' | '4g' | '5g' | 'none' | 'ethernet'
}

const useOnlineHooks = () => {
  const isOnline = ref(true)
  const isWeakLine = ref(false)

  /**
   * @description 设置是否是在线
   * @param {boolean} status
   *  */
  const setIsOnlineStatus = (status: boolean) => {
    isOnline.value = status
  }

  /**
   * @description 设置是否是弱网
   * @param {boolean} status
   *  */
  const setIsWeakLineStatus = (status: boolean) => {
    isWeakLine.value = status
  }

  /**
   *@description  弱网，无网络的条件判断
   *@param {NetworkStatusRes} res
   *  */
  const networkStatusChangeCallBack = (res: NetworkStatusRes) => {
    console.log('AAAAAAAAre', res)

    setIsOnlineStatus(res.isConnected)
    if (res.networkType === '2g') {
      setIsWeakLineStatus(true)
    } else {
      setIsWeakLineStatus(false)
    }
  }

  /**
   * @description 监听网络变化
   *  */
  const onListenNetwork = () => {
    uni.onNetworkStatusChange(networkStatusChangeCallBack)
  }

  /**
   * @description 监听网络变化
   *  */
  const offListenNetwork = () => {
    uni.offNetworkStatusChange(networkStatusChangeCallBack)
  }
  const isOnlineRef = computed(() => isOnline.value)
  const isWeakLineRef = computed(() => isWeakLine.value)

  return {
    onListenNetwork,
    offListenNetwork,
    isOnlineRef,
    isWeakLineRef,
  }
}

export default useOnlineHooks
